<?php

namespace frontend\controllers;
use Yii;
use frontend\models\User;
use frontend\models\LoginForm;
header("content-type:text/html;charset=utf8");
class RegController extends \yii\web\Controller
{
	public $enableCsrfValidation = false; //关闭原有的表单验证类
	public $layout=false;
    
   public function actionIndex(){
    if($post=yii::$app->request->post()){
      // print_r($post);die;
    	 $op=Yii::$app->db->createCommand("SELECT * FROM seven_user WHERE status !=0 and username='".$post['UserName']."'") ->queryOne();
        $user=Yii::$app->db->createCommand("SELECT * FROM seven_user WHERE username='".$post['UserName']."' and password='".md5($post['Password'])."'") ->queryOne();
          if($op){
          	 if($user){
            session_start();
            $_SESSION['user']=serialize($user);

            setcookie('user',serialize($user),time()+7*24*3600);
           	return $this->redirect(['index/index']);

           }else{
           	echo "<script>alert('用户名或密码不对,请重新登录');location.href='index.php?r=reg/index'</script>";die;
            }
          }else{
          	 echo "<script>alert('没有此用户民,请注册');location.href='index.php?r=reg/log'</script>";die;
           }
          }else{
            $ass=new LoginForm();
          return $this->render("login",["ass"=>$ass]);
         }

   }
   public function actions(){   
    return [       
       'captcha' => [
              'class' => 'yii\captcha\CaptchaAction',

              'maxLength' => 5,             
               'minLength' => 5     
                      ],     
                   ];
       }
      public function actionLog()
    {
         if($post=yii::$app->request->post()){
         	if(!empty($post)){
       $op=Yii::$app->db->createCommand("SELECT * FROM seven_user WHERE username='".$post['UserName']."'") ->queryOne();
           if(!$op){
                 if($post['Password']==$post['ConfirmPassword']){
                 $add_time=date('Y-m-d H:i:s');
                $model=new user;
                $model->username=$post['UserName'];
                $model->password=md5($post['Password']);
                $model->status=1;
                $model->user_addtime=$add_time;
                if($model->insert()){
                return 	$this->redirect(['index']);
                }else{
                return $this->render("reg");
                }
              }else{
           echo "<script>alert('两次密码不一致,请重新注册');location.href='index.php?r=reg/log'</script>";
             }

             }else{
           echo "<script>alert('此用户民已存在,请重新注册');location.href='index.php?r=reg/log'</script>";
             }
            }
            }else{
          return $this->render("reg");
         }
       
    }

}?>
